//
//  AlbumDetailViewController.m
//  LastTemp
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AlbumDetailViewController.h"
#import "SingerView.h"
#import "UIImageView+WebCache.h"
#import "NetWorkingTool.h"
#import "AVSingleton.h"
#import "DetailCell.h"
#import "AlbumDetailModel.h"
#import "MJRefresh.h"
#import "bofangViewController.h"
#import "BassModel.h"
@interface AlbumDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *albumTableView;
@property(nonatomic, retain)NSMutableArray *albumDetailArr;
@property(nonatomic, retain)NSMutableArray *passPlayArr;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end

@implementation AlbumDetailViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.albumDetailArr = [NSMutableArray array];
        self.passPlayArr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
//    [_albumDetailArr release];
    [_albumTableView release];
    [_passPlayArr release];
    [_activity release];
    [super dealloc];
}
// 解析数据
- (void)albumDetailData{
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@", self.passAlbumID];
    [NetWorkingTool netWorkingToolStr:str block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        dic1 = dic[@"data"];
        NSMutableArray *arr = [NSMutableArray array];
        arr = dic1[@"songList"];
        for (NSInteger i = 0; i < arr.count; i++) {
            AlbumDetailModel *albumDM = [[AlbumDetailModel alloc] init];
            [albumDM setValuesForKeysWithDictionary:arr[i]];
            NSMutableArray *arr1 = [NSMutableArray array];
            arr1 = arr[i][@"urlList"];
            albumDM.url = [arr1 firstObject][@"url"];
            [self.albumDetailArr addObject:albumDM];
            [albumDM release];
        }
        [self.albumTableView reloadData];
        [self.activity stopAnimating];
    }];
}
// 加载数据
- (void)reloadNewData{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    [self albumDetailData];
    // 顶部View
    SingerView *singerView = [[SingerView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 150)];
    singerView.layer.cornerRadius = 10;
    [self.view addSubview:singerView];
    singerView.singerNameLabel.text = self.passAlbum_name;
    NSURL *url = [NSURL URLWithString:self.passPic_url];
    [singerView.picImageView sd_setImageWithURL:url];
    [singerView release];
    
    // 中下部TableView
    self.albumTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 214, self.view.frame.size.width, self.view.frame.size.height - 264) style:UITableViewStylePlain];
    [self.view addSubview:self.albumTableView];
    [_albumTableView release];
    self.albumTableView.backgroundColor = [UIColor clearColor];
    self.albumTableView.delegate = self;
    self.albumTableView.dataSource = self;
    
    // 右button
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"player" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton:)];
//    UIButton *RightButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    RightButton.frame = CGRectMake(0, 0, 64, 64);
//    [RightButton setBackgroundImage:[UIImage imageNamed:@"bb.gif"] forState:UIControlStateNormal];
//    //    [RightButton setBackgroundImage:[UIImage imageNamed:@"dd"] forState:UIControlStateNormal];
//    [RightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:RightButton] autorelease];
    
    // 进度轮
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];
}
#pragma mark 右button点击方法
- (void)clickRightButton:(UIBarButtonItem *)button{
    bofangViewController *playVC = [[bofangViewController alloc] init];
    playVC.allArr = [AVSingleton shareSingleton].fromArr;
    [self presentViewController:playVC animated:YES completion:^{
    }];
    [playVC release];
}
//#pragma mark 右button点击方法
//- (void)clickRightButton:(UIBarButtonItem *)button{
//    bofangViewController *playVC = [[bofangViewController alloc] init];
//    playVC.allArr = [AVSingleton shareSingleton].fromArr;
//    [self presentViewController:playVC animated:YES completion:^{
//    }];
//    [playVC release];
//}
// 协议必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.albumDetailArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[DetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *str = [NSString stringWithFormat:@"No. %ld %@", indexPath.row + 1, [self.albumDetailArr[indexPath.row] name]];
    cell.nameLabel.text = str;
    cell.singerNameLabel.text = [self.albumDetailArr[indexPath.row] singerName];
    cell.detailImageView.image = [UIImage imageNamed:@"02"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumDetailModel *albumDM =  self.albumDetailArr[indexPath.row];
    if ([AVSingleton shareSingleton].fromNum != indexPath.row || [AVSingleton shareSingleton].fromNum != 0) {
        [[AFSoundManager sharedManager] stop];
    }
    [[AFSoundManager sharedManager] startStreamingRemoteAudioFromURL:albumDM.url andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        [AVSingleton shareSingleton].percentage = percentage;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"mm:ss"];
        NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
        [AVSingleton shareSingleton].beginTime = [formatter stringFromDate:elapsedTimeDate];
        
        NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
        [AVSingleton shareSingleton].time = [formatter stringFromDate:timeRemainingDate];
        
        
    }];

    for (NSInteger i = 0; i < self.albumDetailArr.count; i++) {
        BassModel *baseModel = [[BassModel alloc] init];
        AlbumDetailModel *albumDM1 = self.albumDetailArr[i];
        baseModel.name1 = albumDM1.name;
        baseModel.singerName1 = albumDM1.singerName;
        baseModel.url1 = albumDM1.url;
        [self.passPlayArr addObject:baseModel];
        [baseModel release];
    }
    [AVSingleton shareSingleton].fromNum = indexPath.row;
    [AVSingleton shareSingleton].fromArr = self.passPlayArr;
    [AVSingleton shareSingleton].myBool = YES;
    bofangViewController *play = [[bofangViewController alloc] init];
    [self presentViewController:play animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
